<template>
	<view class="login">
		<view class="content" :class="[attrIndex==1?'content':'content content1']">
			<view class="headtop" :style="{marginTop: mHeight + 'px'}">
				<view class="title">Hello</view>
				<view class="desc">欢迎使用{{x_name}}！</view>
			</view>
			<view class="methon">
				<view class="top clearfix" v-if="attrIndex == 1">
					<view class="left fl">
						<view class="hzsname" @click="qhdlfangshi(1,1)">合作商登录</view>
						<view class="heng"></view>
					</view>
					<view class="left fl">
						<view class="shname" @click="qhdlfangshi(2,1)">商户登录</view>
					</view>
				</view>
				<view class="top clearfix" v-else>
					<view class="left fl">
						<view class="shname" @click="qhdlfangshi(1,1)">合作商登录</view>
					</view>
					<view class="left fl">
						<view class="hzsname" @click="qhdlfangshi(2,1)">商户登录</view>
						<view class="heng"></view>
					</view>
				</view>
				<view class="form" v-if="dlmethon == 1">
					<view class="liebiao">
						<input type="text" placeholder="请输入账号" v-model="username" />
					</view>
					<view class="liebiao" v-if="passhidden">
						<input type="password" placeholder="请输入密码" v-model="password" />
						<image :src="baseUrl + '/wximage/login_xspass.png'" class="yanjing" @click="passhidden = false"></image>
					</view>
					<view class="liebiao" v-else >
						<input type="text" placeholder="请输入密码" v-model="password" />
						<image :src="baseUrl + '/wximage/login_ycpass.png'" class="yanjing" @click="passhidden = true"></image>
					</view>
					<view class="yanzhengma" @click="qhdlfangshi(0,2)">验证码登录</view>
				</view>
				<view class="form" v-if="dlmethon == 2">
					<view class="liebiao">
						<input type="text" placeholder="请输入手机号" v-model="tell" />
					</view>
					<view class="liebiao">
						<input type="text" placeholder="请输入验证码" v-model="yzm" class="inp" />
						<view class="yzm" v-if="time == 60 || time == 0" @click="fasong">获取验证码</view>
						<view class="yzm" v-else>{{time}}S重新获取</view>
					</view>
					<view class="yanzhengma" @click="qhdlfangshi(0,1)">账号登录</view>
				</view>
				<view class="submit" @click="submit">登录</view>
			</view>
		</view>
		<view class="xieyi clearfix">
			<image :src="baseUrl + '/wximage/login_xuanzhong.png'" v-if="xuanzhong == 1" @click="xuanzhong = 2"></image>
			<image :src="baseUrl + '/wximage/login_wxuanzhong.png'" v-else  @click="xuanzhong = 1"></image>
			<view class="desc">
				我已阅读并同意<text @click="topage('/subpackageA/home/second/fuwuxieyi')">《用户服务协议》</text>与<text @click="topage('/subpackageA/home/second/yszhengce')">《隐私保护协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				mHeight: 0,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				xuanzhong: 2,
				attrIndex: 1,
				passhidden: true,
				username: '',
				password: '',
				dlmethon: 1,
				tell: '',
				yzm: '',
				time: 60,
				timer: null,
				x_name: '',
				tjUrl: '',
			}
		},
		onLoad() {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
				
				let that = this;
				uni.getSystemInfo({
				    success: function (res) {
				        let pmheight = res.windowHeight;

						if(pmheight < 600){
							that.mHeight = 68;//45
						}else if(pmheight >= 600 && pmheight < 700){
							that.mHeight = 73;//50
						}else if(pmheight >= 700 && pmheight < 800){
							that.mHeight = 83;//60
						}else if(pmheight >= 800 && pmheight < 830){
							that.mHeight = 110;//97
						}else if(pmheight >= 830 && pmheight < 850){
							that.mHeight = 125;//102
						}else if(pmheight >= 850 && pmheight < 900){
							that.mHeight = 133;//110
						}else{
							that.mHeight = 138;//115
						}
				    },
				})
			}
			
			//获取基本配置信息
			this.request({
				url: '/xcx/webconfig',
				data: {}
			}).then(res => {
				if (res.data.code == 200) {
					this.x_name = res.data.data.x_name;
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
		},
		methods: {
			//登录
			submit(){
				if(this.xuanzhong == 2){
					uni.showToast({
						title: '请先勾选同意协议',
						icon: 'none'
					})
					
					return;
				}
				
				let attrIndex = this.attrIndex;//合作商、商户
				let dlmethon = this.dlmethon;//账号密码、短信
				
				let username = this.username;
				let password = this.password;
				let tell = this.tell;
				let code = this.yzm;
				
				if(dlmethon == 1){
					if (username == '' || password == '') {
						uni.showToast({
							title: '信息不能为空',
							icon: 'none'
						})
						return false;
					}
					
					this.tjUrl = '/xcxlogin/login';
				}else{
					if (tell == '' || code == '') {
						uni.showToast({
							title: '信息不能为空',
							icon: 'none'
						})
						return false;
					}
					
					this.tjUrl = '/xcxlogin/loginyzm';
				}
				
				this.request({
					url: this.tjUrl,
					data: {
						username: username,
						password: password,
						tell: tell,
						code: code,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '登录成功',
							icon: 'none',
						})
						
						uni.setStorageSync('agent_id',res.data.data.id);
						uni.setStorageSync('role_id',res.data.data.role_id)
						
						setTimeout(function(){
							uni.redirectTo({
								url: '/subpackageA/index/index'
							})
						},2000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
				
			},
			fasong(){
				let that =  this;
				
				if( !(/^1[3|4|5|7|8]\d{9}$/.test(this.tell)) ){
					uni.showToast({
						title: '手机号格式有误',
						icon: 'none'
					})
					return false;
				}
				
				this.request({
					url: '/xcx/send_code',
					data: {
						tell: this.tell,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							title: '发送成功',
							icon: 'none'
						})
						
						// 定时60秒
						that.timer = setInterval(() => {
							that.time = that.time - 1;
							if(that.time==0){
								clearTimeout(that.timer);
								that.timer = null;
								that.time = 60;
							}
						},1000);
					} else {
						uni.showToast({
							title: '发送失败',
							icon: 'none'
						})
						return false;
					}
				})
			},
			//切换登录方式
			qhdlfangshi(index,index1){
				if(index){
					this.attrIndex = index;
				}
				
				this.dlmethon = index1;
				
				//清空写入的数据
				this.username = '';
				this.password = '';
				this.tell = '';
				this.time = 60;
				this.yzm = '';
				clearTimeout(this.timer);
				this.timer = null;
			},
			//返回
			fanhui() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			topage(url){
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		width: 100%;
		margin: 0 auto;
		background-image: url('https://api.jixiaodian.cn/images/login_bj.png');
		background-size: 100% 100%;

		.content{
			width: 100%;
			height: 100vh;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 20;
			background-image: url('https://api.jixiaodian.cn/images/login_hzs.png');
			background-size: 100% 100%;

			.headtop{
				width: 90%;
				margin: 0 auto;
				
				.title{
					font-size: 68rpx;
					color: #333333;
					font-weight: 700;
				}
				
				.desc{
					margin-top: 36rpx;
					font-size: 36rpx;
					font-weight: 600;
					color: #333333;
				}
			}
			
			.methon{
				width: 90%;
				margin: 0 auto;
				margin-top: 120rpx;
				
				.top{
					width: 100%;
					
					.left{
						width: 50%;
						text-align: center;
						
						.hzsname{
							font-size: 28rpx;
							color: #144296;
						}
						
						.shname{
							font-size: 28rpx;
							color: #999999;
						}
						
						.heng{
							width: 40rpx;
							margin: 0 auto;
							height: 4rpx;
							background: #144296;
							border-radius: 2rpx;
							margin-top: 10rpx;
						}
					}
				}
				
				.form{
					width: 100%;
					margin-top: 100rpx;
					
					.liebiao{
						width: 100%;
						height: 96rpx;
						line-height: 96rpx;
						background: #F3F5F9;
						border-radius: 50rpx;
						margin-bottom: 30rpx;
						position: relative;
						
						input{
							width: 80%;
							height: 100%;
							padding-left: 70rpx;
							box-sizing: border-box;
							font-size: 32rpx;
							color: #999999;
						}
						
						.inp{
							width: 60%;
						}
						
						.yanjing{
							position: absolute;
							width: 44rpx;
							height: 44rpx;
							top: 26rpx;
							right: 56rpx;
						}
						
						.yzm{
							position: absolute;
							height: 96rpx;
							line-height: 96rpx;
							top: 0;
							right: 56rpx;
							color: #333333;
						}
					}
					
					.yanzhengma{
						text-align: right;
						margin-right: 56rpx;
						color: #5F84F3;
					}
				}
				
				.submit{
					width: 100%;
					margin-top: 60rpx;
					text-align: center;
					height: 96rpx;
					line-height: 96rpx;
					background: linear-gradient(90deg, #85A2F8, #7E9CF8);
					border-radius: 50rpx;
					font-size: 36rpx;
					color: #ffffff;
				}
			}
		}
		
		.content1{
			background-image: url('https://api.jixiaodian.cn/images/login_sh.png');
		}
		
		.xieyi{
			position: fixed;
			width: 84%;
			left: 8%;
			bottom: 100rpx;
			z-index: 30;
			
			image{
				width: 28rpx;
				height: 28rpx;
				margin-right: 10rpx;
				float: left;
				margin-top: 2rpx;
			}
			
			.desc{
				float: left;
				font-size: 24rpx;
				color: #333333;
				
				text{
					font-size: 24rpx;
					color: #5F84F4;
				}
			}
		}
	}
</style>